/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package my12306;

import my12306.http.Consts;
import my12306.table.KeyValueItem;
import org.jdesktop.application.Action;

/**
 *
 * @author Jack
 */
public class OptionsDialog extends javax.swing.JDialog {

    private OptionsBean option;

    /**
     * Creates new form OptionsDialog
     */
    public OptionsDialog(java.awt.Frame parent, OptionsBean option, boolean modal) {
        super(parent, modal);
        initComponents();
        this.option = option;
        if (this.option == null) {
            this.option = new OptionsBean();
        }
        for (KeyValueItem item : Consts.userAgentList) {
            jComboBox1.addItem(item);
        }

        if (this.option.getUserAgent() != null) {
            jComboBox1.setSelectedItem(option.getUserAgent());
        }
        jCheckBox1.setSelected(option.isCheckTicket());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabelUserAgent = new javax.swing.JLabel();
        jLabelSleeptime = new javax.swing.JLabel();
        jLabelKeepAlive = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jSpinnerSleepTime = new javax.swing.JSpinner();
        jSpinnerKeepAliveTime = new javax.swing.JSpinner();
        btnOk = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtSessionID = new javax.swing.JTextField();
        txtBig = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(my12306.My12306App.class).getContext().getResourceMap(OptionsDialog.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.border.title")))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jLabelUserAgent.setText(resourceMap.getString("jLabelUserAgent.text")); // NOI18N
        jLabelUserAgent.setName("jLabelUserAgent"); // NOI18N

        jLabelSleeptime.setText(resourceMap.getString("jLabelSleeptime.text")); // NOI18N
        jLabelSleeptime.setName("jLabelSleeptime"); // NOI18N

        jLabelKeepAlive.setText(resourceMap.getString("jLabelKeepAlive.text")); // NOI18N
        jLabelKeepAlive.setName("jLabelKeepAlive"); // NOI18N

        jComboBox1.setName("jComboBox1"); // NOI18N

        jSpinnerSleepTime.setToolTipText(resourceMap.getString("jSpinnerSleepTime.toolTipText")); // NOI18N
        jSpinnerSleepTime.setName("jSpinnerSleepTime"); // NOI18N
        jSpinnerSleepTime.setValue(7);

        jSpinnerKeepAliveTime.setToolTipText(resourceMap.getString("jSpinnerKeepAliveTime.toolTipText")); // NOI18N
        jSpinnerKeepAliveTime.setName("jSpinnerKeepAliveTime"); // NOI18N
        jSpinnerKeepAliveTime.setValue(5);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(my12306.My12306App.class).getContext().getActionMap(OptionsDialog.class, this);
        btnOk.setAction(actionMap.get("okAction")); // NOI18N
        btnOk.setText(resourceMap.getString("btnOk.text")); // NOI18N
        btnOk.setName("btnOk"); // NOI18N

        btnCancel.setAction(actionMap.get("cancelAction")); // NOI18N
        btnCancel.setText(resourceMap.getString("btnCancel.text")); // NOI18N
        btnCancel.setName("btnCancel"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jCheckBox1.setText(resourceMap.getString("jCheckBox1.text")); // NOI18N
        jCheckBox1.setName("jCheckBox1"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        txtSessionID.setText(resourceMap.getString("txtSessionID.text")); // NOI18N
        txtSessionID.setName("txtSessionID"); // NOI18N

        txtBig.setText(resourceMap.getString("txtBig.text")); // NOI18N
        txtBig.setName("txtBig"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(65, 65, 65)
                .addComponent(btnCancel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(39, 39, 39))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelUserAgent)
                            .addComponent(jLabelSleeptime)
                            .addComponent(jLabelKeepAlive))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtSessionID, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtBig, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jCheckBox1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jSpinnerSleepTime, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                                            .addComponent(jSpinnerKeepAliveTime))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel1)
                                            .addComponent(jLabel2))))
                                .addGap(6, 6, 6))))
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelUserAgent)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelSleeptime)
                    .addComponent(jSpinnerSleepTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelKeepAlive)
                    .addComponent(jSpinnerKeepAliveTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jCheckBox1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtSessionID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtBig, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCancel)
                    .addComponent(btnOk))
                .addGap(22, 22, 22))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void okAction() {
        setVisible(false);
        if (option == null) {
            option = new OptionsBean();
        }
        option.setKeepAlive(Integer.parseInt(jSpinnerKeepAliveTime.getValue().toString()));
        option.setSleepTime(Integer.parseInt(jSpinnerSleepTime.getValue().toString()));
        option.setUserAgent((KeyValueItem) jComboBox1.getSelectedItem());
        option.setCheckTicket(jCheckBox1.isSelected());
        option.setCookies(txtBig.getText().trim(), txtSessionID.getText().trim());
    }

    public OptionsBean showDialog() {
        setVisible(true);
        return option;
    }

    @Action
    public void cancelAction() {
        setVisible(false);
        dispose();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOk;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabelKeepAlive;
    private javax.swing.JLabel jLabelSleeptime;
    private javax.swing.JLabel jLabelUserAgent;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSpinner jSpinnerKeepAliveTime;
    private javax.swing.JSpinner jSpinnerSleepTime;
    private javax.swing.JTextField txtBig;
    private javax.swing.JTextField txtSessionID;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the option
     */
    public OptionsBean getOption() {
        return option;
    }

    /**
     * @param option the option to set
     */
    public void setOption(OptionsBean option) {
        this.option = option;
        if (this.option.getUserAgent() != null) {
            jComboBox1.setSelectedItem(option.getUserAgent());
        }

        jSpinnerSleepTime.setValue(option.getSleepTime());
        jSpinnerKeepAliveTime.setValue(option.getKeepLive());
        jCheckBox1.setSelected(option.isCheckTicket());
        txtBig.setText(option.getBigipServerotsweb());
        txtSessionID.setText(option.getjSessionID());
    }
}
